package com.example.majiang.controller;

import com.example.majiang.dto.QuesitionDTO;
import com.example.majiang.mapper.QuestionMapper;
import com.example.majiang.mapper.UserMappper;
import com.example.majiang.model.Question;
import com.example.majiang.model.User;
import com.example.majiang.service.QuestionService;
import com.example.majiang.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@Controller
public class PublishController {
    @Autowired
    UserUtil userUtil;
    @Autowired
    QuestionService questionService;

    @GetMapping("/publish")
    public String publish(){
        return "publish";
    }
    @PostMapping("/publish")
    public String dpPublish(@RequestParam(value="title",required = false)String title,
                            @RequestParam(value="description",required = false)String description,
                            @RequestParam(value="tag",required = false)String tag,
                            @RequestParam(value = "id", required = false) Integer id,
                            HttpServletRequest request,
                            Model model)
    {
        model.addAttribute("title",title);
        model.addAttribute("description",description);
        model.addAttribute("tag",tag);
        if(title==null||title.equals("")){
            model.addAttribute("error","标题不能为空");
            return "publish";
        }
        if(description==null||description.equals("")){
            model.addAttribute("error","内容不能为空");
            return "publish";
        }
        if(tag==null||tag.equals("")){
            model.addAttribute("error","标签不能为空");
            return "publish";
        }
        User user=null;
        user=(User)request.getSession().getAttribute("user");
        if(user==null){
            model.addAttribute("error","用户必须先登录");
        }

        Question question=new Question();
        question.setDescription(description);
        question.setTitle(title);
        question.setTag(tag);
        question.setId(id);
        question.setCreator(user.getId());
        int i=0;
        questionService.createOrUpdate(question);
        return "redirect:/";
    }

    @GetMapping("/publish/{id}")
    public String edit(@PathVariable(name = "id") Integer id,
                       Model model) {
        QuesitionDTO question = questionService.getById(id);
        model.addAttribute("title", question.getTitle());
        model.addAttribute("description", question.getDescription());
        model.addAttribute("tag", question.getTag());
        model.addAttribute("id", question.getId());
        return "publish";
    }
}
